package center

import (
	"fmt"
)

func get_user_info(user_id int32) *User_info {
	rwlock_uinfo.Lock()
	var user_info *User_info
	_element := user_List.get_element(user_id)
	var data interface{}
	rwlock_uinfo.Unlock()
	if _element != nil {
		data = _element.data
	}
	val, ok := data.(*User_info)
	if ok == true {
		user_info = val
	}
	if user_info == nil {
		fmt.Println("请求的数据不在缓存中", user_id)
		sql := fmt.Sprintf("select * from user_info where user_id=%d", user_id)
		row, err := dbcenter.Read(sql)
		if err != nil {
			fmt.Println("load_user_base error")
			return nil
		}
		if row == nil {
			fmt.Println("user_base no receord")
			return nil
		}
		if row.Next() {
			user_info = new(User_info)
			user_info.genlist = make(map[int32]*Gen_info)
			user_info.baglist = make(map[int32]*Bag_thing)
			user_info.tasklist = make(map[int32]*Task_user)
			row.Scan(&(user_info.user_id), &(user_info.user_code), &(user_info.user_name), &(user_info.headid), &(user_info.country), &(user_info.city), &(user_info.scene_id), &(user_info.silver), &(user_info.gold), &(user_info.cd1), &(user_info.cd2), &(user_info.cd3), &(user_info.user_flag))
			element := new(Element)
			element.data = user_info
			user_List.insert_to_trail(element)
			fmt.Println("user:", user_info)
		}
		row.Close()
	}
	return user_info
}
func get_user_bycode(user_code string) *User_info {

	rwlock_uinfo.Lock()
	var user_info *User_info

	_element := user_List.get_element_bycode(user_code)
	rwlock_uinfo.Unlock()
	var data interface{}
	if _element != nil {
		data = _element.data
	}

	val, ok := data.(*User_info)
	if ok == true {
		user_info = val
	}
	if user_info == nil {
		fmt.Println("请求的数据不在缓存中", user_code)
		sql := fmt.Sprintf("select * from user_info where user_code=%s", user_code)
		row, err := dbcenter.Read(sql)
		if err != nil {
			fmt.Println("load_user_base error")
			return nil
		}
		if row == nil {
			fmt.Println("user_base no receord")
			return nil
		}
		if row.Next() {
			user_info = new(User_info)
			user_info.genlist = make(map[int32]*Gen_info)
			user_info.baglist = make(map[int32]*Bag_thing)
			user_info.tasklist = make(map[int32]*Task_user)
			row.Scan(&(user_info.user_id), &(user_info.user_code), &(user_info.user_name), &(user_info.headid), &(user_info.country), &(user_info.city), &(user_info.scene_id), &(user_info.silver), &(user_info.gold), &(user_info.cd1), &(user_info.cd2), &(user_info.cd3), &(user_info.user_flag))
			element := new(Element)
			element.data = user_info
			user_List.insert_to_trail(element)
			fmt.Println("user:", user_info)
		}
		row.Close()
	}
	return user_info
}
func (user_info *User_info) get_bag_list() map[int32]*Bag_thing {
	if user_info.baglist == nil {
		user_info.baglist = make(map[int32]*Bag_thing)
		row, err := dbcenter.Read("select * from bag_things")
		if err != nil {
			fmt.Println("load bag_thins error")
			return nil
		}
		if row == nil {
			fmt.Println("bag_things no receord")
			return nil
		}
		if row.Next() {
			bag_thing := new(Bag_thing)
			row.Scan(&(bag_thing.bag_thing_id), &(bag_thing.user_id), &(bag_thing.prop_id), &(bag_thing.prop_lv), &(bag_thing.prop_num))
			user_info.baglist[bag_thing.bag_thing_id] = bag_thing
			fmt.Println("bag_thing:", bag_thing)
		}
		row.Close()
	}
	return user_info.baglist
}
func (user_info *User_info) get_gen_list() map[int32]*Gen_info {

	if user_info.genlist == nil {
		user_info.genlist = make(map[int32]*Gen_info)
		row, err := dbcenter.Read("select * from gen_info")
		if err != nil {
			fmt.Println("load user_gen error")
			return nil
		}
		if row == nil {
			fmt.Println("gen_info no receord")
			return nil
		}
		for row.Next() {
			gen_info := new(Gen_info)
			row.Scan(&(gen_info.gen_info_id), &(gen_info.user_id), &(gen_info.lvone_id), &(gen_info.genlv), &(gen_info.attackval), &(gen_info.defensenval))
			fmt.Println("user_gen:", gen_info)
			user_info.genlist[gen_info.gen_info_id] = gen_info
		}
		row.Close()
	}
	return user_info.genlist
}
func (user_info *User_info) get_task_list() map[int32]*Task_user {

	if user_info.tasklist == nil {
		user_info.tasklist = make(map[int32]*Task_user)
		row, err := dbcenter.Read("select * from task_user")
		if err != nil {
			fmt.Println("load_task_user error")
			return nil
		}
		if row == nil {
			fmt.Println("task_user no receord")
			return nil
		}
		if row.Next() {
			task_user := new(Task_user)
			row.Scan(&(task_user.task_user_id), &(task_user.task_id), &(task_user.task_type), &(task_user.user_id), &(task_user.task_status))
			user_info.tasklist[task_user.task_user_id] = task_user
			fmt.Println("task_user:", task_user)
		}
		row.Close()
	}
	return user_info.tasklist
}
